﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Text;
using System.IO;
using System.Dynamic;
using FluorineFx.Json;

namespace lxwmvc
{
    public static partial class huifucontroller
    {
        /// <summary>
        /// 全局引用
        /// </summary>
        /// <param name="pb"></param>
        public static void _init(pagebase pb)
        {
            var user_id = core_user.userID;
            if (user_id.tostr() == "") pb.exit("没有权限操作!");
        }


        /// <summary>
        /// 请求开户的页面，这个页面直接form submit过来。（个人开户）
        /// </summary>
        /// <param name="pb"></param>
        public static void huifu_kaihu(pagebase pb)
        {
            //获取用户信息，包括扩展用户信息
            supermodel userinfo = core_user.getUserInfo();

            dynamic objsend = new UserRegister();
            //传递通知的页面
            objsend.BgRetUrl = HuiFuArg.LocalUrl + "huifu/huifu_kaihu_returnbg";
            objsend.RetUrl = HuiFuArg.LocalUrl + "huifu/huifu_kaihu_return";

            //不能传递这个信息
            //objsend.UsrName = has["truename"].tostr();

            //用户名，这里我们用的是手机号码
            objsend.UsrId = userinfo["MOBIL_NO"] ?? "";
            //00-- 身份证
            objsend.IdType = "00";
            //用户证件号码
            objsend.IdNo = pb.v_s("sfzzh");
            //手机号
            objsend.UsrMp = userinfo["MOBIL_NO"] ?? "";
            //传递参数
            objsend.MerPriv = core_user.userID;
            //用户 Email
            objsend.UsrEmail = userinfo["email"] ?? "";

            //提交到汇付的界面
            core_huifu.Submit(objsend);
        }

        /// <summary>
        /// 开户完毕以后，直接返回的页面
        /// </summary>
        /// <param name="pb"></param>
        public static void huifu_kaihu_return(pagebase pb)
        {
            RetUserRegister objrev = new RetUserRegister();
            //后台调用，最终要打印出来参数
            core_huifu.ReturnUrl(HttpContext.Current, "TRXID", objrev, false);           
            
            //代表成功！
            if (objrev.RespCode == core_huifu.HUIFU_SUCCESS)
            {
                var user = huifu_kaihu_fun(objrev);

                //获取用户的手机号
                var MOBIL_NO = user.select("MOBIL_NO").one<string>();
                
                //发送短信
                core_sms.SendMsg(MOBIL_NO, "亲爱的嘉金投资用户,您于" + DateTime.Now.ToString("F") + "成功开户。若非本人操作，请您联系致电客服。400-100-0616");
                //发送站内信
                core_user.sendMessage(objrev.MerPriv, "亲爱的嘉金投资用户,您于" + DateTime.Now.ToString("F") + "成功开户。若非本人操作，请您联系致电客服。400-100-0616");

                //转到成功页面
                //JSHelper.Redirect("/PUser/message.html?status=1&msg=UserRegister");
            }
            else
            {
                //JSHelper.Redirect("/PUser/message.html?status=0&msg=UserRegister");
            }
        }

        /// <summary>
        /// 开户返回后统一调用方法
        /// </summary>
        /// <param name="objrev"></param>
        /// <returns></returns>
        private static supermodel huifu_kaihu_fun(RetUserRegister objrev)
        {
            int userid = objrev.MerPriv.toint32();

            supermodel.create("SysUser_kuozhan")
                .set("CardType", 1)
                .set("CardNum", objrev.IdNo)
                .set("truename", HttpUtility.UrlDecode(objrev.UsrName))
                .set("kaihuriqi", DateTime.Now)
                .set("UsrId", objrev.UsrId)
                .set("UsrCustId", objrev.UsrCustId)
                .set("TrxId", objrev.TrxId)
                .set("hfflag", 1)
                //根据返回信息，更新用户SysUser_kuozhan
                .where("user_id='" + userid + "'")
                .update();

            var user = supermodel.create("sysuser")
                .set("NICK_NAME", HttpUtility.UrlDecode(objrev.UsrName))
                .set("EMAIL", objrev.UsrEmail)
                .set("USER_NAME", HttpUtility.UrlDecode(objrev.UsrName))
                .set("USER_PRIV", 72)
                .set("DEPT_ID", 302)

                //根据返回信息，更新用户sysuser
                .where("user_id='" + userid + "'");


            user.update();

            //认证成功后奖励红包
            core_user.addhongbao("认证成功页");
            return user;
        }

        /// <summary>
        /// 开户后台返回信息，同时执行多次的时候是否存在问题。。
        /// </summary>
        /// <param name="pb"></param>
        public static void huifu_kaihu_returnbg(pagebase pb)
        {
            RetUserRegister objrev = new RetUserRegister();
            //后台调用，最终要打印出来参数
            core_huifu.ReturnUrl(HttpContext.Current, "TRXID", objrev, true);
           
            //代表成功！
            if (objrev.RespCode == core_huifu.HUIFU_SUCCESS)
            {
                huifu_kaihu_fun(objrev);
            }
        }
    }
}